package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.execption.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.BindTeachplanMediaDto;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * create 2023/5/6 21:40
 */
@Slf4j
@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements TeachplanService {

    @Autowired
    private TeachplanMapper teachplanMapper;

    @Autowired
    private TeachplanMediaMapper teachplanMediaMapper;


    @Override
    public List<TeachplanDto> findTeachplayTree(long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }

    @Override
    public void saveTeachplan(SaveTeachplanDto teachplan) {
        Long id = teachplan.getId();

        if (id == null) {
            //添加
            Teachplan teachplanNew = new Teachplan();
            BeanUtils.copyProperties(teachplan, teachplanNew);
            Integer count= getTeachplanCount(teachplan.getCourseId(), teachplan.getParentid());
            teachplanNew.setOrderby(count + 1);
            teachplanMapper.insert(teachplanNew);
        }else {
            //修改
            Teachplan teachPlan = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(teachplan, teachPlan);
            teachplanMapper.updateById(teachPlan);
        }

    }

    @Override
    public void deleteTeachplan(Long teachPlanId) {
        Teachplan teachplan = this.getById(teachPlanId);
        Long parentid = teachplan.getParentid();
        //1.如果是小节直接删除
        if (!parentid.equals(0L)) {
            this.removeById(teachPlanId);
        }else {
            //1.如果是章节，查看是否有小节，
            LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Teachplan::getParentid, teachPlanId);
            int count = this.count(wrapper);
            if (count > 0) {
                //有小节不能删除
                XueChengPlusException.cast("该章节关联小节，删除失败！");
            }
            this.removeById(teachPlanId);
        }

    }

    @Override
    public void moveUpTeachplan(Long id) {
        Teachplan teachplanUp = this.getById(id);
        Integer orderby = teachplanUp.getOrderby();
        Long parentid = teachplanUp.getParentid();
        if (orderby == 1) {
            XueChengPlusException.cast("已经是排第一，不能再上移！");
        }
        teachplanUp.setOrderby(orderby-1);
        LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teachplan::getOrderby, orderby - 1);
        wrapper.eq(Teachplan::getParentid,parentid);
        if (teachplanUp.getParentid() == 0L) {
            //操作的是章节,需要根据该课程确定章节信息
            wrapper.eq(Teachplan::getCourseId,teachplanUp.getCourseId());
        }
        Teachplan teachplanDown = this.getOne(wrapper);
        teachplanDown.setOrderby(orderby);
        this.updateBatchById(Arrays.asList(teachplanUp, teachplanDown));
    }

    @Override
    public void movedownTeachplan(Long id) {
        Teachplan teachplanDown = this.getById(id);
        Integer orderby = teachplanDown.getOrderby();
        Long parentid = teachplanDown.getParentid();
        Long courseId = teachplanDown.getCourseId();
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();

        if (parentid == 0L) {
            //操作的是章节,查询该课程下有多少个章节
            queryWrapper.eq(Teachplan::getCourseId, courseId);
        }else {
            //操作的是小节，查询该章节下有多少个小节
            queryWrapper.eq(Teachplan::getParentid, parentid);
        }

        int count = this.count(queryWrapper);
        if (count == orderby) {
            XueChengPlusException.cast("已经是最底层，不能再下移！");
        }
        teachplanDown.setOrderby(orderby+1);
        LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teachplan::getOrderby, orderby + 1);
        wrapper.eq(Teachplan::getParentid,parentid);
        if (parentid == 0L) {
            //操作的是章节,需要根据该课程确定章节信息
            wrapper.eq(Teachplan::getCourseId,courseId);
        }
        Teachplan teachplanUp = this.getOne(wrapper);
        teachplanUp.setOrderby(orderby);
        this.updateBatchById(Arrays.asList(teachplanUp, teachplanDown));
    }

    @Override
    public void associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = this.getById(teachplanId);
        if (teachplan == null) {
            XueChengPlusException.cast("该课程计划不存在，请刷新重试！");
        }

        Integer grade = teachplan.getGrade();
        if (grade !=2) {
            XueChengPlusException.cast("只有小节才能绑定视频！");
        }
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setCourseId(teachplan.getCourseId());
        teachplanMedia.setCreateDate(LocalDateTime.now());
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMedia.setTeachplanId(teachplanId);
        teachplanMedia.setMediaId(bindTeachplanMediaDto.getMediaId());
        teachplanMediaMapper.insert(teachplanMedia);

    }

    @Override
    public int  deleteTeachplanByCourseId(Long courseId) {
        LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
         wrapper.eq(Teachplan::getCourseId, courseId);
        int delete = teachplanMapper.delete(wrapper);
        return delete;


    }

    private Integer getTeachplanCount(Long courseId, Long parentid) {

        LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teachplan::getCourseId, courseId);
        wrapper.eq(Teachplan::getParentid, parentid);
        return teachplanMapper.selectCount(wrapper);
    }
}
